package leetcode;

import java.util.Arrays;

public class twelvetwo {
    public static int minOperations(int[] nums, int x) {
        int n=nums.length;
        int sum= Arrays.stream(nums).sum();
        int target=sum-x;
        int max=0;
        int left;
        int right;
        int sum1=0;
        if(target==0){
            return n;
        }
        if(target<0){
            return -1;
        }
        for(left=0,right=0;right<n;right++){
            sum1+=nums[right];
            if(sum1==target){
                max=Math.max(max,right-left+1);
            }
            while(sum1>target){
                sum1-=nums[left];
                left++;
            }
            if(sum1==target){
                max=Math.max(max,right-left+1);
            }

        }
        if(n-max==n){
            return -1;
        }
        return n-max;


    }

    public static void main(String[] args) {
        int[] array={3,2,20,1,1,3};
        int i = minOperations(array, 10);
        System.out.println(i);
    }
}
